package tdh.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tdh.common.util.Util;
import tdh.entity.po.User;
import tdh.entity.vo.Result;
import tdh.entity.vo.UserVo;
import tdh.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户信息控制层
 * @author 曹昊
 * @date 2021/7/12
 */
@Controller
public class InfoController {
    //用户信息业务处理对象
    @Autowired
    UserService userService;
    //HttpServletRequest
    @Autowired
    HttpServletRequest request;

    /**
     * @Author caohao
     * @Description 响应跳转登录页面
     * @Date 14:47 2021/7/12
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/login")
    public String login(){
        return "index";
    }

    /**
     * @Author caohao
     * @Description 响应返回登录是否成功的结果
     * @Date 14:47 2021/7/12
     * @Param [username, password]
     * @return java.lang.String
     **/
    @RequestMapping("/loginCheck")
    public String loginCheck(String username, String password){
        boolean loginCheck = false;
        try {
            loginCheck = userService.loginCheck(username, password);
        }catch (RuntimeException e){
            loginCheck = false;
        }
        if (loginCheck){
            request.getSession().setAttribute("USER",username);
            return "info";
        }else {
            return "index";
        }
    }

    /**
     * @Author caohao
     * @Description 响应返回全部用户信息
     * @Date 14:47 2021/7/12
     * @Param []
     * @return tdh.entity.vo.Result
     **/
    @RequestMapping("/users")
    @ResponseBody
    public Result users(){
        List<UserVo> users = userService.allUsers();
        if (users==null) {
            return Result.error();
        }else {
            return Result.success(users);
        }
    }

    /**
     * @Author caohao
     * @Description 根据用户的部门查询用户列表
     * @Date 14:48 2021/7/12
     * @Param [bmdm]
     * @return tdh.entity.vo.Result
     **/
    @RequestMapping("bmdmUsers")
    @ResponseBody
    public Result bmmcDeparts(String bmdm){
        Result result;
        try {
            result = Result.success(userService.allUsersByBmmc(bmdm));
        }catch (RuntimeException e){
            result = Result.error(e.getMessage());
        }
        return result;
    }

    /**
     * @Author caohao
     * @Description 根据用户代码查询指定用户请求
     * @Date 14:49 2021/7/12
     * @Param [yhdm]
     * @return tdh.entity.vo.Result
     **/
    @RequestMapping("yhdmUser")
    @ResponseBody
    public Result yhdmUser(String yhdm){

        UserVo userVo = null;
        try {
            userVo = userService.userByYhdm(yhdm);
        }catch (RuntimeException e){
            userVo = null;
        }
        if (userVo==null){
            return Result.error();
        }else{
            return Result.success(userVo);
        }
    }

    /**
     * @Author caohao
     * @Description 跳转新增页面请求
     * @Date 15:24 2021/7/12
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("add")
    public String addJSP(){
        return "add";
    }

    /**
     * @Author caohao
     * @Description 新增用户请求
     * @Date 14:50 2021/7/12
     * @Param [user]
     * @return tdh.entity.vo.Result
     **/
    @RequestMapping("insertUser")
    @ResponseBody
    public Result insertUser(String userJson){
        Result result;
        try {
            userJson = Util.jsonTsf(userJson);
            User user = JSONObject.parseObject(userJson, User.class);
            String yhdm = Util.BMDM +user.getYhid();
            user.setYhdm(yhdm);
            userService.insertOne(user);
            result = Result.success("");
        }catch (RuntimeException e){
            result = Result.error(e.getMessage());
        }
        return result;
    }

    /**
     * @Author caohao
     * @Description 删除用户请求
     * @Date 14:51 2021/7/12
     * @Param [yhdm]
     * @return tdh.entity.vo.Result
     **/
    @RequestMapping("deleteOne")
    @ResponseBody
    public Result deleteOne(String yhdm){
        Result result;
        try {
            userService.deleteOne(yhdm);
            List<UserVo> userVos = userService.allUsers();
            result = Result.success(userVos);
        }catch (RuntimeException e){
            result = Result.error(e.getMessage());
        }
        return result;
    }

    /**
     * @Author caohao
     * @Description 更新用户请求
     * @Date 14:51 2021/7/12
     * @Param [user]
     * @return tdh.entity.vo.Result
     **/
    @RequestMapping("updateUser")
    @ResponseBody
    public Result updateUser(String userJson){
        Result result;
        try {
            userJson = Util.jsonTsf(userJson);
            User user = JSONObject.parseObject(userJson,User.class);
            System.out.println(user.toString());
            String yhdm = Util.BMDM +user.getYhid();
            user.setYhdm(yhdm);
            userService.updateUser(user);
            result = Result.success("");
        }catch (RuntimeException e){
            result = Result.error(e.getMessage());
        }
        return result;
    }

    /**
     * @Author caohao
     * @Description 根据用户姓名查询用户
     * @Date 15:25 2021/7/12
     * @Param [yhxm]
     * @return tdh.entity.vo.Result
     **/
    @RequestMapping("yhxmUsers")
    @ResponseBody
    public Result yhxmUsers(String yhxm){
        Result result;
        try {
            List<UserVo> userVos;
            userVos = userService.allUsersByYhid(yhxm);
            if (userVos.size()==0) {
                userVos = userService.yhxmUsers(yhxm);
                result = Result.success(userVos);
            }else {
                result = Result.success(userVos);
            }
        }catch (RuntimeException e){
            result = Result.error(e.getMessage());
        }
        return result;
    }

    /**
     * @Author caohao
     * @Description 注销登录请求
     * @Date 14:51 2021/7/12
     * @Param []
     * @return tdh.entity.vo.Result
     **/
    @RequestMapping("logout")
    @ResponseBody
    public Result logout(){
        try {
            request.getSession().removeAttribute("USER");
        }catch (RuntimeException e){
        }
        return Result.success("");
    }





}
